unit fFreeFormLoader;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,
  Vcl.ExtCtrls,

  GLS.PersistentClasses,
  GLS.Coordinates,
  GLS.BitmapFont,
  GLS.WindowsFont,
  GLS.Scene,
  GLS.VectorTypes,
  GLS.Objects,
  GLS.SceneViewer,

  GLS.ModelManager,  // .3ds, .obj, .ply

  GLS.BaseClasses,
  GLS.Cadencer,
  GLS.SimpleNavigation;

type
  TFormFFLoader = class(TForm)
    Panel1: TPanel;
    GLSceneViewer1: TGLSceneViewer;
    GLScene1: TGLScene;
    dcMaster: TGLDummyCube;
    GLCamera1: TGLCamera;
    dcScene: TGLDummyCube;
    ButtonAdd: TButton;
    OpenDialog1: TOpenDialog;
    GLLightSource1: TGLLightSource;
    lblCount: TLabel;
    lstModels: TListBox;
    lblProxies: TLabel;
    GLCadencer1: TGLCadencer;
    GLSimpleNavigation1: TGLSimpleNavigation;
    procedure FormCreate(Sender: TObject);
    procedure ButtonAddClick(Sender: TObject);
  private
    DataDir: TFileName;
    CubeSize: Integer;
  public
    Mx, My: Integer;
  end;

var
  FormFFLoader: TFormFFLoader;
  Manager: TGLModelManager; // The model manager

implementation

{$R *.dfm}


procedure TFormFFLoader.FormCreate(Sender: TObject);
begin
  SetCurrentDir(Application.ExeName);
  DataDir := ExtractFilePath(ParamStr(0)) + 'data';
  SetCurrentDir(DataDir);

  Randomize;
  CubeSize := 20;
  // Create the manager : give him a dummycube to store the freeforms
  // and give him the path to the models
  Manager := TGLModelManager.Create(dcMaster, '');
end;

procedure TFormFFLoader.ButtonAddClick(Sender: TObject);
var
  NewProxy: TGLProxyObject;

begin
  // This is the creation of a new proxy
  OpenDialog1.InitialDir := DataDir;
  if (OpenDialog1.Execute) then
  begin
    NewProxy := TGLProxyObject(dcScene.AddNewChild(TGLProxyObject));
    NewProxy.Position.X := Random(CubeSize);
    NewProxy.Position.Y := Random(CubeSize);
    NewProxy.Position.Z := Random(CubeSize);
    // Need a master object for this new proxy ?
    // Just use the manager :
    NewProxy.MasterObject := Manager.LoadModel
      (ExtractFilename(OpenDialog1.FileName));
  end;

  // Keep track of the manager's list
  lblCount.Caption := 'Models in the list : ' + IntToStr(dcMaster.Count);
  lblProxies.Caption := 'Proxies in the scene : ' + IntToStr(dcScene.Count);
  lstModels.Items := Manager.ModelList;
end;

end.
